package com.china08.yxyapi.api.manage.edu.office.outernotice;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeOutter;
import com.china08.yxyapi.entity.mg.office.YxyNoticeOutter.YxyNotifictionSchool;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.mg.YxyNoticeOutterRepository;

/**
 * 教育局行政办公 外部通知 搜索某个通知
 * 
 */
@CrossOrigin
@RestController("OuterNoticeReceiveOneSrhController")
public class OuterNoticeReceiveOneSrhController extends YxyApiBaseController {

    @Autowired
    private YxyNoticeOutterRepository yxyNoticeOutterRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/edu/office/outerNotice/receive/oneSrh", method = RequestMethod.POST)
    public RespModel precess(HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel//
    ) {
        // 当前教育局id
        String currentSchoolId = getProfile(request, response).getSchoolId();
        // 当前用户的获取
        String currentUserId = getProfile(request, response).getId();
        // 查询当前消息
        YxyNoticeOutter yxyNoticeOutter = yxyNoticeOutterRepository.findById(reqModel.id);
        RespModel respModel = new RespModel();
        respModel.setCurrentSchoolId(currentSchoolId);
        respModel.setCurrentSchoolNick("");
        respModel.setCurrentUserId(currentUserId);
        respModel.setCurrentUserNickname(yxyNoticeOutter.getUserNick());
        respModel.setYxyNoticeOutter(yxyNoticeOutter);

        // ##
        // 获取学校名称集合
        List<YxyNotifictionSchool> schools = yxyNoticeOutter.getReceiveSchool();
        String schoolName = "";

        Integer isReadCount = 0;
        Integer noReadCount = 0;
        List<Pojo> isReadList = new ArrayList<Pojo>();
        List<Pojo> noReadList = new ArrayList<Pojo>();

        for (YxyNotifictionSchool yxyNotifictionSchool : schools) {
            schoolName = yxyNotifictionSchool.getSchoolName();
            respModel.schoolNameList.add(schoolName);

            String schoolId = yxyNotifictionSchool.getSchoolId();
            Integer isRead = yxyNotifictionSchool.getIsRead();
            Integer isDelete = yxyNotifictionSchool.isDelete;
            if (schoolId.equals(currentSchoolId) && isRead == 0 && isDelete == 0) {
                yxyNotifictionSchool.setIsRead(1);
            }
            Pojo pojo = new Pojo();
            pojo.schoolName = yxyNotifictionSchool.getSchoolName();
            // 未读
            if (isRead == 0) {
                noReadCount = noReadCount + 1;
                noReadList.add(pojo);
            }
            // 已读
            else {
                isReadCount = isReadCount + 1;
                isReadList.add(pojo);
            }
        }
        respModel.isReadCount = isReadCount;
        respModel.noReadCount = noReadCount;
        respModel.isReadList = isReadList;
        respModel.noReadList = noReadList;

        yxyNoticeOutter.setReceiveSchool(schools);
        yxyNoticeOutterRepository.save(yxyNoticeOutter);

        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

        // 内部通知id
        public String id;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

    }

    public static class Pojo {
        public String userId;
        public String schoolName;
    }

    protected static class RespModel {

        public List<String> schoolNameList = new ArrayList<>();

        // 内部通知详细信息
        public YxyNoticeOutter yxyNoticeOutter;

        // 当前用户id
        public String currentUserId;

        // 当前用户昵称
        public String currentUserNickname;

        // 当前用户教育局id
        public String currentSchoolId;

        // 当前用户教育局昵称
        public String currentSchoolNick;

        public Integer isReadCount;
        public List<Pojo> isReadList;
        public Integer noReadCount;
        public List<Pojo> noReadList;

        public YxyNoticeOutter getYxyNoticeOutter() {
            return yxyNoticeOutter;
        }

        public void setYxyNoticeOutter(YxyNoticeOutter yxyNoticeOutter) {
            this.yxyNoticeOutter = yxyNoticeOutter;
        }

        public String getCurrentUserId() {
            return currentUserId;
        }

        public void setCurrentUserId(String currentUserId) {
            this.currentUserId = currentUserId;
        }

        public String getCurrentUserNickname() {
            return currentUserNickname;
        }

        public void setCurrentUserNickname(String currentUserNickname) {
            this.currentUserNickname = currentUserNickname;
        }

        public String getCurrentSchoolId() {
            return currentSchoolId;
        }

        public void setCurrentSchoolId(String currentSchoolId) {
            this.currentSchoolId = currentSchoolId;
        }

        public String getCurrentSchoolNick() {
            return currentSchoolNick;
        }

        public void setCurrentSchoolNick(String currentSchoolNick) {
            this.currentSchoolNick = currentSchoolNick;
        }
    }
}
